package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    //展示
    @LogAnnotation(content = "展示用户",type = "select")
    @ResponseBody
    @RequestMapping("show")
    public Map showAll(int page,int limit,String uname){
        Map map = new HashMap();
        PageInfo<User> pageInfo = userService.showAll(page, limit, uname);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }

    //修改状态
    @LogAnnotation(content = "修改用户状态",type = "updateStatus")
    @ResponseBody
    @RequestMapping("updateStatus")
    public Map updateStatus(int userId,int userStatus){
        Map map = new HashMap();
        try {
            userService.updateState(userId, userStatus);
            map.put("isUpdate",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate",false);
        }
        return  map;
    }




    @ResponseBody
    @RequestMapping("insertMany")
    public void insertMany(){
        System.out.println("方法启动");
        userService.addMany();
        System.out.println("方法jieshu-----------------------------------");
    }

    //按照性别查询用户统计
    @LogAnnotation(content = "按照性别查询用户统计",type = "selectBySex")
    @ResponseBody
    @RequestMapping("selectBySex")
    public List<Map> selectBySex(){
        List<Map> list = userService.showBySex();
        System.out.println("list = " + list);
        return  list;
    }

    //按照时间查询用户统计
    @LogAnnotation(content = "按照时间查询用户统计",type = "selectByTime")
    @ResponseBody
    @RequestMapping("selectByTime")
    public List<Map> selectByTime(){
        List<Map> list = new ArrayList<>();

        List<Map> list1 = userService.showByTime(1,7);
        List<Map> list2 = userService.showByTime(7,14);
        List<Map> list3 = userService.showByTime(14,21);
        list1.get(0).put("name","过去一周");
        list2.get(0).put("name","过去两周");
        list3.get(0).put("name","过去三周");
        list.add(list1.get(0));
        list.add(list2.get(0));
        list.add(list3.get(0));
        System.out.println("list = " + list);
        return  list;
    }


    //按照省份查询用户统计
    @LogAnnotation(content = "按照省份查询用户统计",type = "selectByProvince")
    @ResponseBody
    @RequestMapping("selectByProvince")
    public List<Map> selectByProvince(){
        List<Map> maps = userService.showByProvince();
        System.out.println("maps = " + maps);
        return  maps;
    }



    //查询用户统计（未优化）
    @LogAnnotation(content = "查询用户统计（未优化）",type = "select")
    @ResponseBody
    @RequestMapping("selectAll")
    public Map selectAll(){
        long time1 = System.currentTimeMillis();
        Map map = new HashMap();
        //按照性别查询用户统计
        List<Map> listSex = userService.showBySex();
        map.put("sex",listSex);

        //按照时间查询用户统计
        List<Map> listTime = new ArrayList<>();

        List<Map> list1 = userService.showByTime(1,7);
        List<Map> list2 = userService.showByTime(7,14);
        List<Map> list3 = userService.showByTime(14,21);
        list1.get(0).put("name","过去一周");
        list2.get(0).put("name","过去两周");
        list3.get(0).put("name","过去三周");
        listTime.add(list1.get(0));
        listTime.add(list2.get(0));
        listTime.add(list3.get(0));
        map.put("time",listTime);

        //按照省份查询用户统计
        List<Map> listProvince = userService.showByProvince();
        map.put("province",listProvince);
        long time2 = System.currentTimeMillis();
        System.out.println("time1 = " + time1);
        System.out.println("time2 = " + time2);
        System.out.println(time2-time1+"ms");

        return map;
    }


}
